
package com.ifcar.app.model;

import com.ifcar.app.db.DBUtils;
import com.ifcar.app.db.NewsSQLiteOpenHelper;
import com.ifcar.app.rss.GetColumnsInfo;
import com.ifcar.app.util.StringUtil;
import com.ifcar.app.util.Tools;
import com.ifcar.app.activity.R;

import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewsColumn {
    public static List<Map<String, Object>> getColumnListFromDB(Context context, SQLiteDatabase db) {
        List<Map<String, Object>> columnsListData = new ArrayList<Map<String, Object>>();

        Cursor cursor = Tools.getDataFromDB(context, NewsSQLiteOpenHelper.TOP_COLUMN_TABLE, db);
        if (cursor != null) {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put(GetColumnsInfo.TNAME_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.TNAME)));
                map.put(GetColumnsInfo.TID_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.TID)));
                map.put(GetColumnsInfo.CID_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.CID)));
                columnsListData.add(map);
            }
            cursor.close();
        }

        return columnsListData;
    }

    public static List<Map<String, Object>> getColumnListFromNet(Context context, SQLiteDatabase db) {
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();

        List<HashMap<String, Object>> tmpColumnsListData = null;
        // 从网络取
        if (Tools.CheckNetwork(context)) {
            tmpColumnsListData = GetColumnsInfo.getDefaultColumns(context);
        }

        
        if (tmpColumnsListData != null && tmpColumnsListData.size() > 0) {
            
            
            List<Map<String, Object>> topColumnList = getColumnListFromDB(context,db);
            
            List<HashMap<String, Object>> tmpNewColumnsListData = new ArrayList<HashMap<String, Object>>();
            boolean theSameTid = false;
            for (int k = 0; k < tmpColumnsListData.size(); k++) {
                
                String tid = StringUtil.GetString(((HashMap<String, Object>) tmpColumnsListData.get(k)).get(NewsSQLiteOpenHelper.TID));
                theSameTid = false;
                for (int j = 0; j < topColumnList.size(); j++) {
                    
                    String toptid = StringUtil.GetString(((HashMap<String, Object>) topColumnList.get(j)).get(NewsSQLiteOpenHelper.TID));
                    
                    if(!TextUtils.isEmpty(tid) && !TextUtils.isEmpty(toptid) && toptid.equalsIgnoreCase(tid)){
                        theSameTid = true;
                    }
                }
                
                if(!theSameTid){
                    tmpNewColumnsListData.add(tmpColumnsListData.get(k));
                }
            }
            
            db.beginTransaction();
            
            
            db.execSQL("delete from " + NewsSQLiteOpenHelper.MORE_COLUMN_TABLE);
            
            for (int k = 0; k < tmpNewColumnsListData.size(); k++) {
                String tname = ((HashMap<String, Object>) tmpNewColumnsListData.get(k)).get(
                        NewsSQLiteOpenHelper.TNAME).toString();
                String tid = ((HashMap<String, Object>) tmpNewColumnsListData.get(k)).get(
                        NewsSQLiteOpenHelper.TID).toString();
                String cid = ((HashMap<String, Object>) tmpNewColumnsListData.get(k)).get(
                        NewsSQLiteOpenHelper.CID).toString();
                
                if(topColumnList.size()==0){
                    
                    if(k<5){
                        Tools.insertDataToDB(NewsSQLiteOpenHelper.TOP_COLUMN_TABLE, db, tname, tid,cid);
                    }else{
                        Tools.insertDataToDB(NewsSQLiteOpenHelper.MORE_COLUMN_TABLE, db, tname, tid,cid);
                    }
                    
                }else{
                    Tools.insertDataToDB(NewsSQLiteOpenHelper.MORE_COLUMN_TABLE, db, tname, tid,cid);
                }
                
                
                
               
            }
            db.setTransactionSuccessful();
            db.endTransaction();
            StringUtil.updateRefreshTime(context, "column_list");
        }

        return result;
    }
    
    public static List<Map<String, Object>> getDefaultColumnList(Context context, SQLiteDatabase db) {
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        Resources res = context.getResources();
        String[] first_columns = res.getStringArray(R.array.first_column_array);
        String[] first_columns_tid = res.getStringArray(R.array.first_column_tid_array);

        db.beginTransaction();
        for (int i = 0; i < first_columns.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put(GetColumnsInfo.TNAME_TAG, first_columns[i]);
            map.put(GetColumnsInfo.TID_TAG, first_columns_tid[i]);
            Tools.insertDataToDB(NewsSQLiteOpenHelper.TOP_COLUMN_TABLE, db, first_columns[i],
                    first_columns_tid[i], "");
            result.add(map);
        }

        String[] second_columns = res.getStringArray(R.array.second_column_array);
        String[] second_columns_tid = res.getStringArray(R.array.second_column_tid_array);

        for (int i = 0; i < second_columns.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put(GetColumnsInfo.TNAME_TAG, second_columns[i]);
            map.put(GetColumnsInfo.TID_TAG, second_columns_tid[i]);

            Tools.insertDataToDB(NewsSQLiteOpenHelper.MORE_COLUMN_TABLE, db, second_columns[i],
                    second_columns_tid[i], "");
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        
        return result;

    }
}
